package com.linkoog.devtools.code_generator.action;

import com.intellij.openapi.actionSystem.ActionUpdateThread;
import com.intellij.openapi.actionSystem.AnActionEvent;
import com.intellij.openapi.actionSystem.DefaultActionGroup;
import com.intellij.openapi.actionSystem.LangDataKeys;
import com.intellij.openapi.actionSystem.Presentation;
import com.intellij.openapi.module.Module;
import com.intellij.openapi.project.Project;
import com.intellij.openapi.roots.ProjectRootManager;
import com.intellij.openapi.vfs.VirtualFile;
import org.jetbrains.annotations.NotNull;


public class GenCodeActionGroup extends DefaultActionGroup {

    public GenCodeActionGroup() {
        super();
    }


    @Override
    public void update(@NotNull AnActionEvent event) {
        Project project = event.getProject();
        Module module = event.getData(LangDataKeys.MODULE);
        VirtualFile actionFolder = event.getData(LangDataKeys.VIRTUAL_FILE);
        Presentation presentation = event.getPresentation();
        if (project != null && module != null && actionFolder != null && actionFolder.isDirectory()) {
            // Set visibility based on if the package name is non-null
            String packageName = ProjectRootManager.getInstance(project).getFileIndex().getPackageNameByDirectory(actionFolder);
            presentation.setPopupGroup(true);
            presentation.setVisible(packageName != null);
        } else {
            presentation.setVisible(false);
        }
    }

    @Override
    public @NotNull ActionUpdateThread getActionUpdateThread() {
        return ActionUpdateThread.BGT;
    }

}
